package com.sunop.panticommon.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {

    @Bean(name = "commonThreadPool")
    public ThreadPoolTaskExecutor commonThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);               // 核心线程数
        executor.setMaxPoolSize(20);               // 最大线程数
        executor.setQueueCapacity(100);            // 队列容量
        executor.setKeepAliveSeconds(60);          // 空闲线程存活时间(秒)
        executor.setThreadNamePrefix("CommonPool-");
        executor.setRejectedExecutionHandler(
            new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略，直接在调用线程中运行
        );
        executor.initialize();
        return executor;
    }
}